﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Semantics.Silverlight
{
    public partial class LanguageNodeControl : TreeView
    {
        Window Owner
        {
            get {
                object parent = this.Parent;
                while (!(parent is Window))
                    parent = ((Control)parent).Parent;
                return (Window)parent;
            }
        }
        public LanguageNodeControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        public bool EnableCheckBoxes { get; set; }

        public bool? GetTreeViewItemChecked(TreeViewItem treeitem)
        {
            if (!this.EnableCheckBoxes)
                throw new Exception("The checkboxes is not enabled!");
            StackPanel sp = (StackPanel)treeitem.Header;
            CheckBox checkbox = (CheckBox)sp.Children[0];
            return checkbox.IsChecked;
        }
        public LanguageNode RootNode
        {
            get
            {
                if (this.Tag == null)
                    return null;
                else
                    return (LanguageNode)this.Tag;
            }
            set
            {
                this.Tag = value;
                if (value == null)
                    this.Items.Clear();
                else
                    this.ShowRootLangNode(value);
            }
        }

        TreeViewItem CreateTreeViewItem(string text)
        {
            TreeViewItem treenode = new TreeViewItem();
            if (this.EnableCheckBoxes)
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Horizontal;
                CheckBox cb = new CheckBox();
                //cb.Content = text;
                sp.Children.Add(cb);
                System.Windows.Controls.TextBlock label = new System.Windows.Controls.TextBlock();
                label.Text = text;
                sp.Children.Add(label);
                treenode.Header = sp;//关键就是在 这个 Header
            }
            else
                treenode.Header = text;
            return treenode;
        }
        public TreeViewItem ShowRootLangNode(LanguageNode root)
        {
            TreeViewItem rootnode = CreateTreeViewItem(root.ToString());
            this.Items.Add(rootnode);
            //rootnode.ImageIndex = Reference.GetLangNodeImage(root);
            rootnode.Tag = root;
            for (int i = 0; i < root.Children.Count; i++)
            //for (int i = 0; i < root.Children.Count; i++)
            {
                LanguageNode child = (LanguageNode)root.Children[i];
                TreeViewItem childtreenode = ShowLangNode(child, rootnode.Items, 1);
                //if (child.Attribute != null)
                //    childtreenode.Text += "(" + child.Conception.Value + " as " + child.Attribute + ")";
            }
            rootnode.IsExpanded = true;
            return rootnode;
        }

        public TreeViewItem ShowLangNode(LanguageNode node, ItemCollection ParentTreeViewItems, int deepth)
        {
            if (deepth > 16)
                throw new Exception("Too deep");
            TreeViewItem treenode = CreateTreeViewItem(node.ToString());
            ParentTreeViewItems.Add(treenode);
            treenode.Tag = node;
            for (int i = 0; i < node.Children.Count; i++)
            {
                LanguageNode child = (LanguageNode)node.Children[i];
                TreeViewItem childtreenode = ShowLangNode(child, treenode.Items, deepth + 1);
                //if (connection.Attribute != null && connection.Conception != null)
                //{
                //    childtreenode.Text += "(" + connection.Conception.Value + " as " + connection.Attribute + ")";
                //}
            }

            treenode.IsExpanded=true;
            return treenode;
        }
        public List<LanguageNode> FindSelectedNodes()
        {
            List<LanguageNode> selectednodes = new List<LanguageNode>();
            foreach (TreeViewItem node in this.Items)
            {
                FindSelectedNodes(node, selectednodes);
            }
            return selectednodes;
        }
        void FindSelectedNodes(TreeViewItem node, List<LanguageNode> selectednodes)
        {
            if (this.GetTreeViewItemChecked(node).Value)
            {
                selectednodes.Add((LanguageNode)node.Tag);
            }
            foreach (TreeViewItem childnode in node.Items)
                FindSelectedNodes(childnode, selectednodes);
        }
        public List<TreeViewItem> FindSelectedTreeNodes()
        {
            List<TreeViewItem> selectedtreenodes = new List<TreeViewItem>();
            foreach (TreeViewItem rootnode in this.Items)
                FindSelectedTreeNodes(rootnode, selectedtreenodes);
            if (selectedtreenodes.Count < 2)
            {
                MessageBox.Show("请选择两个以上连续的节点!");
                return null;
            }
            ItemCollection items=null;
            if(selectedtreenodes[0].Parent is TreeView)
                items=this.Items;
            else
                items=((TreeViewItem)selectedtreenodes[0].Parent).Items;
            LanguageNode firstlangnode=(LanguageNode)selectedtreenodes[0].Tag;
            int begin = items.IndexOf(selectedtreenodes[0]);
            LanguageNode prev = (LanguageNode)selectedtreenodes[0].Tag;
            for (int i = 1; i < selectedtreenodes.Count; i++)
            {
                TreeViewItem nextitem=selectedtreenodes[i];
                if (nextitem.Parent != selectedtreenodes[0].Parent)
                {
                    MessageBox.Show(this.Owner, "必须选择兄弟节点！");
                    return null;
                }
                LanguageNode next = (LanguageNode)selectedtreenodes[i].Tag;
                int index = items.IndexOf(nextitem);
                if (index != begin + i)
                {
                    if ((prev.AbsolutePos + prev.Length) != next.AbsolutePos)
                    {
                        MessageBox.Show(this.Owner, "必须选择相邻的兄弟节点！");
                        return null;
                    }
                    else
                    {
                        if (MessageBox.Show(this.Owner, "节点''和节点''词序上相邻，但在TreeControl不相邻，确认要组合?",
                            "确认", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                            return null;
                    }
                }
                prev = next;
            }
            return selectedtreenodes;
        }
        void FindSelectedTreeNodes(TreeViewItem current, List<TreeViewItem> selectedtreenodes)
        {
            if (this.GetTreeViewItemChecked(current).Value)
                selectedtreenodes.Add(current);
            foreach (TreeViewItem childtreenode in current.Items)
                FindSelectedTreeNodes(childtreenode, selectedtreenodes);
        }
    }
}
